import java.util.concurrent.*;


public class IntController {
  // The interrupt controler component has a private semaphore to maintain 
  // interrupt requests coming from all other components. 
  // Interruptions from memory are exceptions that need to be handled right
  // now, and have priority over other ints. So, memory interrupt has its
  // own indicator, and the others compete among them using the Semaphore.
  
  private final int memoryInterruptNumber = 3;
  private final int size = 10;
  private final int no_interruption = 0;
  private String tabs = "\t\t\t\t\t\t\t\t"; 	  
  private LinkedBlockingDeque<Integer> fila;
	
  IntController() {
		fila = new LinkedBlockingDeque<Integer>(size);
	}
	
	public int get()
	{
		if(fila.isEmpty())
			return no_interruption;
		else
			try {
				return fila.take();
			} catch (InterruptedException e) {
				System.err.println(tabs + "Problem when taking interruption");
				e.printStackTrace();
				return -1;
			}
			
	}
	
	public void set(int n)
	{
		
		if(fila.size() < size)
		{
			System.err.println(tabs + " HARD INT " + n);
			if(n == memoryInterruptNumber) //Priority to memory
				fila.addFirst(n);
			else
				fila.add(n);
		}
		else
			System.err.println(tabs + "Interruption queue is full! INT not set");
	}
}
